home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / MPlex.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.6 KB  |  164 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.net.InetAddress;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. import java.util.Vector;
  9. import symjava.sql.SQLException;
  10.  
  11. class MPlex {
  12.    Vector _IOs;
  13.    InetAddress _host;
  14.    int _port;
  15.    boolean _shareSocket;
  16.    int _connectionID;
  17.    ClientSession _sess;
  18.    boolean _connLost;
  19.  
  20.    MPlex(InetAddress host, int port, boolean shareSocket, ClientSession sess) {
  21.       this._host = host;
  22.       this._port = port;
  23.       this._shareSocket = shareSocket;
  24.       this._sess = sess;
  25.       this._IOs = new Vector();
  26.       this._connLost = false;
  27.    }
  28.  
  29.    void connect() throws NetException, ErrorException {
  30.       SessionRequest s = new SessionRequest(this._sess);
  31.       this._connectionID = s.connect();
  32.    }
  33.  
  34.    void disconnect() {
  35.       try {
  36.          SessionRequest s = new SessionRequest(this._sess);
  37.          s.disconnect();
  38.       } catch (Exception var2) {
  39.       }
  40.    }
  41.  
  42.    short getNextID() throws NetException {
  43.       for(short i = 0; i < this._IOs.size(); ++i) {
  44.          if ((IO)this._IOs.elementAt(i) == null) {
  45.             return i;
  46.          }
  47.       }
  48.  
  49.       if (this._IOs.size() < 32765) {
  50.          return (short)this._IOs.size();
  51.       } else {
  52.          throw new NetException("IO object limit reached");
  53.       }
  54.    }
  55.  
  56.    Socket getSocket() throws NetException {
  57.       Socket s = null;
  58.       if (this._shareSocket && this._IOs.size() != 0) {
  59.          IO io = (IO)this._IOs.elementAt(0);
  60.          s = io._sock;
  61.       } else {
  62.          if (this._port < 80) {
  63.             throw new NetException("Port# must be 80 or higher");
  64.          }
  65.  
  66.          try {
  67.             s = new Socket(this._host, this._port);
  68.          } catch (UnknownHostException var3) {
  69.             throw new NetException("Unknown host");
  70.          } catch (IOException var4) {
  71.             throw new NetException("Server not responding");
  72.          } catch (SecurityException var5) {
  73.             throw new NetException("Security exception");
  74.          }
  75.       }
  76.  
  77.       return s;
  78.    }
  79.  
  80.    synchronized IO getIO() throws NetException, SQLException {
  81.       if (this._connLost) {
  82.          throw new SQLServerConnException();
  83.       } else {
  84.          short id = this.getNextID();
  85.          Socket s = this.getSocket();
  86.          IO io;
  87.          if (this._shareSocket && this._IOs.size() != 0) {
  88.             IO sysIO = (IO)this._IOs.elementAt(0);
  89.             io = new IO(sysIO._writer, sysIO.getInputStream(), id);
  90.          } else {
  91.             io = new IO(this, s, id);
  92.          }
  93.  
  94.          if (id == this._IOs.size()) {
  95.             this._IOs.addElement(io);
  96.          } else {
  97.             this._IOs.setElementAt(io, id);
  98.          }
  99.  
  100.          if (id != 0 && !this._shareSocket) {
  101.             try {
  102.                SessionRequest sess = new SessionRequest(this._sess);
  103.                sess.reconnect(io, this._connectionID);
  104.             } catch (ErrorException var5) {
  105.                throw new NetException("Error reconnecting IO object");
  106.             }
  107.          }
  108.  
  109.          return io;
  110.       }
  111.    }
  112.  
  113.    synchronized void close() {
  114.       for(short i = 0; i < this._IOs.size(); ++i) {
  115.          if ((IO)this._IOs.elementAt(i) != null) {
  116.             ((IO)this._IOs.elementAt(i)).close();
  117.          }
  118.       }
  119.  
  120.       this._connLost = true;
  121.    }
  122.  
  123.    synchronized void releaseIO(IO io) {
  124.       if (io._id != 0) {
  125.          io.close();
  126.          this._IOs.setElementAt((Object)null, io._id);
  127.       }
  128.  
  129.    }
  130.  
  131.    synchronized OutputStream getStream(short streamID) {
  132.       IO io = (IO)this._IOs.elementAt(streamID);
  133.       return io._readerOS;
  134.    }
  135.  
  136.    synchronized void connectionLost() {
  137.       this._connLost = true;
  138.       this._sess.lostConnection();
  139.    }
  140.  
  141.    synchronized void connectionLost(SocketReader r) {
  142.       SocketWriter wtr = null;
  143.  
  144.       for(short i = 0; i < this._IOs.size(); ++i) {
  145.          IO io = (IO)this._IOs.elementAt(i);
  146.          if (io != null && io._reader == r) {
  147.             wtr = io._writer;
  148.             break;
  149.          }
  150.       }
  151.  
  152.       if (wtr != null) {
  153.          for(int i = 0; i < this._IOs.size(); ++i) {
  154.             IO io = (IO)this._IOs.elementAt(i);
  155.             if (io != null && io._writer == wtr) {
  156.                NetPipedInputStream str = (NetPipedInputStream)io._userIS;
  157.                str.connBroken();
  158.             }
  159.          }
  160.  
  161.       }
  162.    }
  163. }
  164.